// @ts-nocheck
import router from '@system.router';
import LogUtil from '../../common/baseUtil/LogUtil.js';
import BaseParseConfModel from '../../model/baseParseConfImpl/BaseParseConfModel.js';
import batteryStatistics from '@ohos.batteryStatistics';
import prompt from '@ohos.prompt';

let baseParseConfModel = new BaseParseConfModel();
let logUtil = new LogUtil();
let list = [
    {
        "settingIcon": "/res/image/icon_thermal.png",
        "settingType": 1,
        "settingAlias": "getBatteryStats",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_thermal.png",
        "settingType": 1,
        "settingAlias": "getAppPowerValue",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_thermal.png",
        "settingType": 1,
        "settingAlias": "getAppPowerPercent",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_thermal.png",
        "settingType": 1,
        "settingAlias": "getHardwareUnitPowerValue",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_thermal.png",
        "settingType": 1,
        "settingAlias": "getHardwareUnitPowerPercent",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    }
];

export default {
    data: {
        statsList: [],
        uidList: [],
        uidIndex: 0,
        uidValue: "",
        statsJsonPath: '/data/app/el1/bundle/public/com.example.powerhap/com.example.entry/assets/power/resources/rawfile/statsList.json'
    },
    onInit() {
        this.statsList = baseParseConfModel.getJsonData(this.statsJsonPath);
        this.statsList = this.statsList || list
        logUtil.info('statsPage get statsList by json:' + JSON.stringify(this.statsList))
        for (let key in this.statsList) {
            var settingAlias = this.statsList[key].settingAlias
            this.statsList[key].settingTitle = this.$t('strings.'.concat(settingAlias))
        }
        logUtil.info('statsPage onInit end:')
    },

    onclickStats(item) {
        switch (item.detail.text.settingAlias) {
            case 'getBatteryStats':
                break;
            case 'getAppPowerValue':
                logUtil.info('statsPage getAppPowerValue begin')
                this.getAllUid()
                this.$element("getAppPowerValue").show();
                break;
            case 'getAppPowerPercent':
                logUtil.info('statsPage getAppPowerPercent begin')
                this.getAllUid()
                this.$element("getAppPowerPercent").show();
                break;
            case 'getHardwareUnitPowerValue':
                logUtil.info('statsPage getHardwareUnitPowerValue begin')
                this.getAllType()
                this.$element("getHardwareUnitPowerValue").show();
                break;
            case 'getHardwareUnitPowerPercent':
                logUtil.info('statsPage getHardwareUnitPowerPercent begin')
                this.getAllType()
                this.$element("getHardwareUnitPowerPercent").show();
                break;
            default:
                break;
        }
    },

    getAllUid() {
        this.uidList = ['0']
        batteryStatistics.getBatteryStats().then((list) => {
            logUtil.info('statsPage getAllUid Promise begin')
            list.forEach((item) => {
                if (item.uid != -1) {
                    this.uidList.push(item.uid)
                    logUtil.info('statsPage getAllUid Promise uid' + item.uid)
                }
            })
        }).catch(e => {
            prompt.showToast({
                message: 'get all uid code: ' + e.code + ' msg: ' + e.message,
                duration: 3000,
            });
        })
    },

    getAppPowerValue({ newValue: newValue }) {
        let power = this.promptAction(() => {return batteryStatistics.getAppPowerValue(Number(newValue))})
        this.statsList[1].settingValue = power
        logUtil.info('statsPage getAppPowerValue end uid: ' + newValue + ', power: ' + power)
    },
    getAppPowerPercent({ newValue: newValue }) {
        let power = this.promptAction(() => {return batteryStatistics.getAppPowerPercent(Number(newValue))})
        this.statsList[2].settingValue = power
        logUtil.info('statsPage getAppPowerPercent end uid: ' + newValue + ', power: ' + power)
    },
    getHardwareUnitPowerValue({ newValue: newValue }) {
        let power = this.promptAction(() => {return batteryStatistics.getHardwareUnitPowerValue(Number(newValue))})
        this.statsList[3].settingValue = power
        logUtil.info('statsPage getHardwareUnitPowerValue end type: ' + newValue + ', power: ' + power)
    },
    getHardwareUnitPowerPercent({ newValue: newValue }) {
        let power = this.promptAction(() => {return batteryStatistics.getHardwareUnitPowerPercent(Number(newValue))})
        this.statsList[4].settingValue = power
        logUtil.info('statsPage getHardwareUnitPowerPercent end type: ' + newValue + ', power: ' + power)
    },

    getAllType() {
        logUtil.info('statsPage getAllType begin')
        this.uidList = ['0']
        batteryStatistics.getBatteryStats((error, list) => {
            if (typeof error !== "undefined") {
                prompt.showToast({
                    message: 'callback: ' + 'code: ' + error.code + ' msg: ' + error.message,
                    duration: 3000,
                });
                return;
            }
            logUtil.info('statsPage getAllType callback' + list)
            if (list == null || list == undefined) {
                return;
            }
            logUtil.info('statsPage getAllType callback begin')
            list.forEach((item) => {
                if (item.uid == -1) {
                    this.uidList.push(item.type)
                    logUtil.info('statsPage getAllUid callback uid' + item.type)
                }
            })
            logUtil.info('statsPage getAllType callback end')
        });
        logUtil.info('statsPage getAllType end')
    },

    promptAction (func) {
        let ret = null;
        try {
            ret = func();
            let isPromise = (typeof ret === 'object') && (typeof ret.then === 'function')
            console.log("is isPromise: " + isPromise)
            if (isPromise) {
                ret.then((data) => {
                    console.log("is isPromise data: " + data)
                }).catch((error) => {
                    prompt.showToast({
                        message: 'code: ' + error.code + ' msg: ' + error.message,
                        duration: 3000,
                    });
                })
            }
        } catch (e) {
            prompt.showToast({
                message: 'code: ' + e.code + ' msg: ' + e.message,
                duration: 3000,
            });
        }
        return ret;
    },

    pullDown() {
    },
    statsBack() {
        router.back();
    },
    onBackPress() {
        router.back();
    },
    onCreate() {
    },
    onReady() {
    },
    onShow() {
    },
    onHide() {
    },
    onDestroy() {
    }
}
